home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / Play3DSound / Sound3D.frm < prev    next >
Text File  |  2001-10-08  |  16KB  |  498 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form DS3DPositionForm 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "DS 3D Positioning"
  6.    ClientHeight    =   5565
  7.    ClientLeft      =   930
  8.    ClientTop       =   330
  9.    ClientWidth     =   5055
  10.    Icon            =   "Sound3D.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   5565
  15.    ScaleWidth      =   5055
  16.    Begin VB.Timer tmrUpdate 
  17.       Interval        =   50
  18.       Left            =   4260
  19.       Top             =   2100
  20.    End
  21.    Begin MSComDlg.CommonDialog cdlFile 
  22.       Left            =   3780
  23.       Top             =   2040
  24.       _ExtentX        =   847
  25.       _ExtentY        =   847
  26.       _Version        =   393216
  27.    End
  28.    Begin VB.PictureBox picDraw 
  29.       BackColor       =   &H00FFFFFF&
  30.       FillStyle       =   7  'Diagonal Cross
  31.       Height          =   2775
  32.       Left            =   120
  33.       ScaleHeight     =   181
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   317
  36.       TabIndex        =   7
  37.       TabStop         =   0   'False
  38.       Top             =   2640
  39.       Width           =   4815
  40.    End
  41.    Begin VB.PictureBox picContainer 
  42.       Height          =   1755
  43.       Index           =   0
  44.       Left            =   120
  45.       ScaleHeight     =   1695
  46.       ScaleWidth      =   4755
  47.       TabIndex        =   10
  48.       TabStop         =   0   'False
  49.       Top             =   120
  50.       Width           =   4815
  51.       Begin VB.TextBox txtSound 
  52.          BackColor       =   &H8000000F&
  53.          Height          =   315
  54.          Left            =   960
  55.          Locked          =   -1  'True
  56.          TabIndex        =   13
  57.          Top             =   120
  58.          Width           =   3735
  59.       End
  60.       Begin VB.CommandButton cmdSound 
  61.          Caption         =   "Sound..."
  62.          Enabled         =   0   'False
  63.          Height          =   315
  64.          Left            =   60
  65.          TabIndex        =   0
  66.          Top             =   120
  67.          Width           =   855
  68.       End
  69.       Begin VB.CommandButton cmdPlay 
  70.          Caption         =   "Play"
  71.          Height          =   375
  72.          Left            =   120
  73.          TabIndex        =   3
  74.          Top             =   1200
  75.          Width           =   855
  76.       End
  77.       Begin VB.CommandButton cmdPause 
  78.          Caption         =   "Pause"
  79.          Height          =   375
  80.          Left            =   1020
  81.          TabIndex        =   4
  82.          Top             =   1200
  83.          Width           =   855
  84.       End
  85.       Begin VB.CommandButton cmdStop 
  86.          Caption         =   "Stop"
  87.          Height          =   375
  88.          Left            =   1920
  89.          TabIndex        =   5
  90.          Top             =   1200
  91.          Width           =   735
  92.       End
  93.       Begin VB.CheckBox chLoop 
  94.          Caption         =   "Loop Play"
  95.          Height          =   315
  96.          Left            =   2760
  97.          TabIndex        =   6
  98.          Top             =   1260
  99.          Width           =   1455
  100.       End
  101.       Begin VB.HScrollBar scrlVol 
  102.          Height          =   255
  103.          LargeChange     =   20
  104.          Left            =   840
  105.          Max             =   0
  106.          Min             =   -3000
  107.          SmallChange     =   500
  108.          TabIndex        =   1
  109.          Top             =   540
  110.          Width           =   3855
  111.       End
  112.       Begin VB.HScrollBar scrlAngle 
  113.          Height          =   255
  114.          LargeChange     =   20
  115.          Left            =   840
  116.          Max             =   360
  117.          Min             =   -360
  118.          SmallChange     =   10
  119.          TabIndex        =   2
  120.          Top             =   840
  121.          Value           =   -90
  122.          Width           =   3855
  123.       End
  124.       Begin VB.Label Label1 
  125.          BackStyle       =   0  'Transparent
  126.          Caption         =   "Volume"
  127.          Height          =   255
  128.          Index           =   0
  129.          Left            =   120
  130.          TabIndex        =   12
  131.          Top             =   600
  132.          Width           =   1095
  133.       End
  134.       Begin VB.Label Label2 
  135.          BackStyle       =   0  'Transparent
  136.          Caption         =   "Direction"
  137.          Height          =   255
  138.          Index           =   0
  139.          Left            =   120
  140.          TabIndex        =   11
  141.          Top             =   900
  142.          Width           =   975
  143.       End
  144.    End
  145.    Begin VB.Label Label5 
  146.       BackStyle       =   0  'Transparent
  147.       Caption         =   "Click and drag the red triangle around with the left mouse button to change the sound position."
  148.       Height          =   495
  149.       Left            =   120
  150.       TabIndex        =   9
  151.       Top             =   2160
  152.       Width           =   4755
  153.    End
  154.    Begin VB.Label Label4 
  155.       BackStyle       =   0  'Transparent
  156.       Caption         =   "Sound Positions"
  157.       BeginProperty Font 
  158.          Name            =   "MS Sans Serif"
  159.          Size            =   8.25
  160.          Charset         =   0
  161.          Weight          =   700
  162.          Underline       =   0   'False
  163.          Italic          =   0   'False
  164.          Strikethrough   =   0   'False
  165.       EndProperty
  166.       Height          =   375
  167.       Left            =   120
  168.       TabIndex        =   8
  169.       Top             =   1920
  170.       Width           =   1575
  171.    End
  172. End
  173. Attribute VB_Name = "DS3DPositionForm"
  174. Attribute VB_GlobalNameSpace = False
  175. Attribute VB_Creatable = False
  176. Attribute VB_PredeclaredId = True
  177. Attribute VB_Exposed = False
  178. Option Explicit
  179. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  180. '
  181. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  182. '
  183. '  File:       Sound3d.frm
  184. '
  185. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  186.  
  187. 'API declare for windows folder
  188. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  189.  
  190. Dim dx As New DirectX8 'Our DirectX object
  191. Dim ds As DirectSound8 'Our DirectSound object
  192. Dim dsBuffer As DirectSoundSecondaryBuffer8 'Our SoundBuffer
  193. Dim ds3dBuffer As DirectSound3DBuffer8 'We need to get a 3DSoundBuffer
  194. Dim oPos As D3DVECTOR 'Position
  195. Dim fMouseDown As Boolean 'Is the mouse down?
  196.  
  197. Private Sub cmdSound_Click()
  198.  
  199.     Static sCurDir As String
  200.     Static lFilter As Long
  201.     Dim dsBuf As DSBUFFERDESC
  202.     
  203.     'Now we should load a wave file
  204.     'Ask them for a file to load
  205.     With cdlFile
  206.         .flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly
  207.         .FilterIndex = lFilter
  208.         .Filter = "Wave Files (*.wav)|*.wav"
  209.         .FileName = vbNullString
  210.         If sCurDir = vbNullString Then
  211.             'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  212.             Dim sWindir As String
  213.             sWindir = Space$(255)
  214.             If GetWindowsDirectory(sWindir, 255) = 0 Then
  215.                 'We couldn't get the windows folder for some reason, use the c:\
  216.                 .InitDir = "C:\"
  217.             Else
  218.                 Dim sMedia As String
  219.                 sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  220.                 If Right$(sWindir, 1) = "\" Then
  221.                     sMedia = sWindir & "Media"
  222.                 Else
  223.                     sMedia = sWindir & "\Media"
  224.                 End If
  225.                 If Dir$(sMedia, vbDirectory) <> vbNullString Then
  226.                     .InitDir = sMedia
  227.                 Else
  228.                     .InitDir = sWindir
  229.                 End If
  230.             End If
  231.         Else
  232.             .InitDir = sCurDir
  233.         End If
  234.         .ShowOpen   ' Display the Open dialog box
  235.         If .FileName = vbNullString Then
  236.             Exit Sub 'We didn't click anything exit
  237.         End If
  238.         'Save the current information
  239.         sCurDir = GetFolder(.FileName)
  240.         lFilter = .FilterIndex
  241.         
  242.         'Save the filename for later use
  243.         If Not (dsBuffer Is Nothing) Then dsBuffer.Stop
  244.         Set dsBuffer = Nothing
  245.         txtSound.Text = vbNullString
  246.         dsBuf.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_CTRLVOLUME
  247.         'Before we load the 3D dialog check to see if this is a mono file
  248.         On Error Resume Next
  249.         Set dsBuffer = ds.CreateSoundBufferFromFile(.FileName, dsBuf)
  250.         If Err Then
  251.             'First check to see if this is a stereo wav file
  252.             If (dsBuf.fxFormat.nChannels > 1) And (Err.Number = 5) Then 'Yup
  253.                 MsgBox "You must load a mono wave file to control 3D sound.  Stereo wave files are not supported.", vbOKOnly Or vbInformation, "Couldn't load"
  254.             Else
  255.                 MsgBox "Could not load this wave file." & vbCrLf & "Format is not supported.", vbOKOnly Or vbInformation, "Couldn't load"
  256.             End If
  257.             Exit Sub
  258.         End If
  259.         
  260.         'Now we need to get the 3D virtualization params
  261.         Dim f3DParams As New frm3DAlg
  262.         
  263.         f3DParams.Show vbModal, Me
  264.         If f3DParams.OKHit Then
  265.             If f3DParams.optFull Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_HRTF_FULL
  266.             If f3DParams.optHalf Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_HRTF_LIGHT
  267.             If f3DParams.optNone Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_NO_VIRTUALIZATION
  268.         Else
  269.             Set dsBuffer = Nothing
  270.             Exit Sub
  271.         End If
  272.         On Error Resume Next
  273.         Set dsBuffer = ds.CreateSoundBufferFromFile(.FileName, dsBuf)
  274.         If Err Then
  275.             MsgBox "Could not create the sound buffer.", vbOKOnly Or vbInformation, "Couldn't load"
  276.             Exit Sub
  277.         End If
  278.         txtSound.Text = .FileName
  279.         EnablePlayUI True
  280.         Set ds3dBuffer = dsBuffer.GetDirectSound3DBuffer
  281.         ds3dBuffer.SetConeAngles DS3D_MINCONEANGLE, 100, DS3D_IMMEDIATE
  282.         ds3dBuffer.SetConeOutsideVolume -400, DS3D_IMMEDIATE
  283.         ' position our sound
  284.         ds3dBuffer.SetPosition oPos.x / 50, 0, oPos.z / 50, DS3D_IMMEDIATE
  285.         'Update the volume
  286.         scrlVol_Change
  287.     End With
  288.     
  289. End Sub
  290.  
  291. Private Sub Form_Load()
  292.     
  293.     
  294.     On Local Error Resume Next
  295.     Set ds = dx.DirectSoundCreate(vbNullString) 'Create a default DirectSound object
  296.     'We couldn't create the DSound object.  End the app now
  297.     If Err.Number <> 0 Then
  298.         MsgBox "Could not initialize DirectSound." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  299.         Unload Me
  300.         End
  301.     End If
  302.     'Set the coop level
  303.     ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
  304.     
  305.     'Show the form
  306.     Me.Show
  307.     oPos.x = 0: oPos.z = 5
  308.     '- Make sure we pickup the correct volume and orientation
  309.     scrlAngle_Change
  310.     scrlVol_Change
  311.     
  312.     DrawPositions
  313.     EnablePlayUI True
  314.     cmdPlay.Enabled = False
  315.     cmdSound.SetFocus
  316. End Sub
  317.  
  318. Private Sub cmdPlay_Click()
  319.     If dsBuffer Is Nothing Then Exit Sub
  320.            
  321.     'Play plays the sound from the current position
  322.     'if the sound was paused using the stop command
  323.     'then play will begin where it last left off
  324.     dsBuffer.Play chLoop.Value 'Checked = 1 (looping), Unchecked = 0 (Default)
  325.     EnablePlayUI False
  326. End Sub
  327.  
  328. Private Sub cmdStop_Click()
  329.     If dsBuffer Is Nothing Then Exit Sub
  330.     
  331.     dsBuffer.Stop
  332.     dsBuffer.SetCurrentPosition 0 'Reset the position since Stop doesn't
  333.     EnablePlayUI True
  334. End Sub
  335.  
  336. Private Sub cmdPause_Click()
  337.     If dsBuffer Is Nothing Then Exit Sub
  338.     dsBuffer.Stop 'Stop doesn't reset the position
  339. End Sub
  340.  
  341. 'They've changed the volume.  Update it
  342. Private Sub scrlVol_Change()
  343.     If dsBuffer Is Nothing Then Exit Sub
  344.     dsBuffer.SetVolume scrlVol.Value
  345. End Sub
  346.  
  347. Private Sub scrlVol_Scroll()
  348.     scrlVol_Change
  349. End Sub
  350.  
  351. 'They've changed the angle.  Update it
  352. Private Sub scrlAngle_Change()
  353.     
  354.     'We need to calculate a vector of what direction the sound is traveling in.
  355.     Dim x As Single
  356.     Dim z As Single
  357.     'we take the current angle in degrees convert to radians
  358.     'and get the cos or sin to find the direction from an angle
  359.     x = 5 * Cos(3.141 * scrlAngle.Value / 180)
  360.     z = 5 * Sin(3.141 * scrlAngle.Value / 180)
  361.     
  362.     'Update the UI
  363.     DrawPositions
  364.     If dsBuffer Is Nothing Then Exit Sub
  365.     ds3dBuffer.SetConeOrientation x, 0, z, DS3D_IMMEDIATE
  366.     
  367. End Sub
  368.  
  369. Private Sub scrlAngle_Scroll()
  370.     scrlAngle_Change
  371. End Sub
  372.  
  373. Sub UpdatePosition(x As Single, z As Single)
  374.     On Error Resume Next
  375.     oPos.x = x - picDraw.ScaleWidth / 2
  376.     oPos.z = z - picDraw.ScaleHeight / 2
  377.     
  378.     DrawPositions
  379.     
  380.     'the zero at the end indicates we want the postion updated immediately
  381.     If ds3dBuffer Is Nothing Then Exit Sub
  382.     
  383.     ds3dBuffer.SetPosition oPos.x / 50, 0, oPos.z / 50, DS3D_IMMEDIATE
  384.     
  385. End Sub
  386.  
  387. Private Sub picDraw_MouseDown(Button As Integer, Shift As Integer, x As Single, z As Single)
  388.     On Error Resume Next
  389.     If Button = vbLeftButton Then
  390.         UpdatePosition x, z
  391.         fMouseDown = True
  392.     End If
  393. End Sub
  394.  
  395. Private Sub picDraw_MouseMove(Button As Integer, Shift As Integer, x As Single, z As Single)
  396.     On Error Resume Next
  397.     If Not fMouseDown Then Exit Sub
  398.     If Button = vbLeftButton Then
  399.         'Only update the position if it is outside of the box
  400.         If x < 0 Or z < 0 Or x > picDraw.ScaleWidth Or z > picDraw.ScaleHeight Then Exit Sub
  401.         UpdatePosition x, z
  402.     End If
  403. End Sub
  404.  
  405. Private Sub picDraw_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
  406.     On Error Resume Next
  407.     fMouseDown = False
  408. End Sub
  409.  
  410. Private Sub picDraw_Paint()
  411.     DrawPositions
  412. End Sub
  413.  
  414. Sub DrawPositions()
  415.     Dim x As Integer
  416.     Dim z As Integer
  417.     
  418.     picDraw.Cls
  419.     
  420.     'listener is in center and is black
  421.     DrawTriangle 0, picDraw.ScaleWidth / 2, picDraw.ScaleHeight / 2, 90
  422.     
  423.     'draw sound as RED
  424.     x = CInt(oPos.x) + picDraw.ScaleWidth / 2
  425.     z = CInt(oPos.z) + picDraw.ScaleHeight / 2
  426.     DrawTriangle RGB(256, 0, 0), x, z, scrlAngle.Value
  427.     
  428. End Sub
  429.  
  430. 'Draw a triangle representing where we are
  431. Sub DrawTriangle(col As Long, x As Integer, z As Integer, ByVal a As Single)
  432.     
  433.     Dim x1 As Integer
  434.     Dim z1 As Integer
  435.     Dim x2 As Integer
  436.     Dim z2 As Integer
  437.     Dim x3 As Integer
  438.     Dim z3 As Integer
  439.     
  440.     a = 3.141 * (a - 90) / 180
  441.     Dim q As Integer
  442.     q = 10
  443.     
  444.     x1 = q * Sin(a) + x
  445.     z1 = q * Cos(a) + z
  446.     
  447.     x2 = q * Sin(a + 3.141 / 1.3) + x
  448.     z2 = q * Cos(a + 3.141 / 1.3) + z
  449.     
  450.     x3 = q * Sin(a - 3.141 / 1.3) + x
  451.     z3 = q * Cos(a - 3.141 / 1.3) + z
  452.     
  453.     picDraw.Line (x1, z1)-(x2, z2), col
  454.     picDraw.Line (x1, z1)-(x3, z3), col
  455.     picDraw.Line (x2, z2)-(x3, z3), col
  456. End Sub
  457.  
  458. Private Function GetFolder(ByVal sFile As String) As String
  459.     Dim lCount As Long
  460.     
  461.     For lCount = Len(sFile) To 1 Step -1
  462.         If Mid$(sFile, lCount, 1) = "\" Then
  463.             GetFolder = Left$(sFile, lCount)
  464.             Exit Function
  465.         End If
  466.     Next
  467.     GetFolder = vbNullString
  468. End Function
  469.  
  470. Private Sub EnablePlayUI(ByVal fEnable As Boolean)
  471.     On Error Resume Next
  472.     If fEnable Then
  473.         chLoop.Enabled = True
  474.         cmdPlay.Enabled = True
  475.         cmdPause.Enabled = False
  476.         cmdStop.Enabled = False
  477.         cmdSound.Enabled = True
  478.         cmdPlay.SetFocus
  479.     Else
  480.         chLoop.Enabled = False
  481.         cmdPlay.Enabled = False
  482.         cmdStop.Enabled = True
  483.         cmdPause.Enabled = True
  484.         cmdSound.Enabled = False
  485.         cmdStop.SetFocus
  486.     End If
  487. End Sub
  488.  
  489. Private Sub tmrUpdate_Timer()
  490.     If Not (dsBuffer Is Nothing) Then
  491.         If (dsBuffer.GetStatus And DSBSTATUS_PLAYING) <> DSBSTATUS_PLAYING Then
  492.             If cmdPlay.Enabled = False Then
  493.                 EnablePlayUI True
  494.             End If
  495.         End If
  496.     End If
  497. End Sub
  498.